﻿using AppWebService;
using Newtonsoft.Json;
using SelfService.Model.Interface;
using SelfService.Model.Models;
using SelfService.XinXing.Models;

namespace SelfService.XinXing
{
    public class Order : IHisOrder
    {
        private readonly AutoWebServiceSoapClient _appClient;

        public Order(AppWebService.AutoWebServiceSoapClient appClient)
        {
            _appClient = appClient;
        }

        public Task<ResultDetail<List<IOrder>>> GetOrderList(IPatient patient)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 缴费
        /// </summary>
        /// <param name="order"></param>
        /// <param name="pay"></param>
        /// <param name="client"></param>
        /// <param name="operator"></param>
        /// <returns></returns>
        public Task<ResultDetail<bool>> Pay(IOrder order, SysPay pay, SysClient client, SysOperator @operator)
        {
            throw new NotImplementedException();
        }

        public Task<ResultDetail<bool>> Pay(IOrder order, SysPay pay)
        {
            throw new NotImplementedException();
        }

        public async Task<ResultDetail<bool>> Refund(SysPay pay)
        {
            throw new NotImplementedException();
        }


        /// <summary>
        /// 充值
        /// </summary>
        /// <param name="pay"></param>
        /// <param name="client"></param>
        /// <param name="operator"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ResultDetail<bool>> AddCredit(SysPay pay, SysClient client, SysOperator @operator)
        {
            var request = new HisCreditRequest();
            request.CardNo = pay.Patient.CardNo;
            request.TotalAmount = pay.Amount;
            request.TradeNo = pay.OutOrderNo;
            request.OutTradeNo = pay.OutOrderNo;
            request.Jyrq = pay.CreateTime.ToString("yyyy-MM-dd HH:mm:ss");
            request.OperatorId = @operator.Code;
            request.UserId = @operator.Code;
            request.Zffs = "0E";
            request.Ip = client.Ip;
            var xml = request.ToXml();
            try
            {
                var response = await _appClient.GetValueAsync(xml);
                if (response.XmlTryParseToObject(out HisCreditResponse result))
                {
                    if (result.ResultCode == "0")
                    {
                        return new ResultDetail<bool>
                        {
                            Request = xml,
                            Response = response
                        };
                    }
                    else
                    {
                        return new ResultDetail<bool>
                        {
                            Code = 1,
                            Message = result.ErrorMsg,
                            Request = xml,
                            Response = response
                        };
                    }
                }
                return new ResultDetail<bool>
                {
                    Code = 1,
                    Message = "充值结果未知, 返回数据解析异常",
                    Request = xml,
                    Response = response
                };
            }
            catch (Exception e)
            {
                return new ResultDetail<bool>
                {
                    Code = 1,
                    Message = $"充值结果未知, 未知异常:{JsonConvert.SerializeObject(e)}",
                    Request = xml
                };
            }
        }

        public Task<ResultDetail<string>> CreateTreatmentNo(IPatient patient)
        {
            throw new NotImplementedException();
        }

        public Task<ResultDetail<SysMedicalProject[]>> GetProjectList()
        {
            throw new NotImplementedException();
        }

        public Task<ResultDetail<IOrder>> CreateOrder(IPatient patient, string projectNo, int qty)
        {
            throw new NotImplementedException();
        }

        public Task<ResultDetail<bool>> Refund(SysPrescription prescription)
        {
            throw new NotImplementedException();
        }


        public Task<ResultDetail<IOrder>> CreateOrder(IPatient patient, string projectNo)
        {
            throw new NotImplementedException();
        }

        public Task<ResultDetail<bool>> CreateOrder(string patientId, string treatmentNo, string projectNo)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="patient"></param>
        /// <param name="client"></param>
        /// <param name="operator"></param>
        /// <returns></returns>
        public Task<ResultDetail<List<IOrder>>> GetOrderList(IPatient patient, SysClient client, SysOperator @operator)
        {
            throw new NotImplementedException();
        }
    }
}
